package com.kasax.skin.support.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Window;

import androidx.appcompat.app.AlertDialog;

import com.kasax.skin.support.R;

import java.lang.ref.WeakReference;


/**
 * 对话框工具类
 */
public class DialogUtils {

    private static AlertDialog.Builder dialogBuilder;

    public static interface Callback {
        void onConfirm();

        void onCancel();
    }


    public static class CommonCallbackImpl implements Callback {

        @Override
        public void onConfirm() {

        }

        @Override
        public void onCancel() {

        }
    }

    /**
     * 不需要回调的情况
     */
    public static void show(Activity activity, String title, String msg, String okayMsg) {
        show(activity, title, msg, okayMsg, null, null);
    }

    /**
     * 不需要取消的情况
     */
    public static void show(Activity activity, String title, String msg, String okayMsg, Callback callback) {
        show(activity, title, msg, okayMsg, null, callback);
    }

    public static void show(Activity activity, String title, String msg, String okayMsg, String cancelMsg,
                            Callback callback) {
        WeakReference<Activity> activityWeakReference = new WeakReference<>(activity);
        dialogBuilder =
                new AlertDialog.Builder(activity, R.style.NoTitleDialogTheme).setTitle(title).setMessage(msg);
        if (okayMsg != null && !TextUtils.isEmpty(okayMsg)) {
            dialogBuilder.setPositiveButton(okayMsg, (dialog, which) -> {
                dialog.dismiss();
                if (callback != null) {
                    callback.onConfirm();
                }
            });
        }
        if (cancelMsg != null && !TextUtils.isEmpty(cancelMsg)) {
            dialogBuilder.setNegativeButton(cancelMsg, (dialog, which) -> {
                dialog.dismiss();
                if (callback != null) {
                    callback.onCancel();
                }
            });
        }
        AlertDialog alertDialog = dialogBuilder.create();
        // 防止出现现行title
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (activityWeakReference.get() != null && !activityWeakReference.get().isFinishing()) {
            alertDialog.show();
        }
    }
}
